package gatech.cs3251.project4;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.Toast;

public class FriendTrackerController extends Activity {
	
	private CheckBox netcommcheckbox;
	private Intent startupIntent = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.v("CHRIS", "In FriendTrackerController onCreate");
	    setContentView(R.layout.main);
		
		netcommcheckbox = (CheckBox) findViewById(R.id.netcommcheckbox);
		if(FriendTracker.isRunning == true)
		{
			netcommcheckbox.setChecked(true);
		}
		netcommcheckbox.setOnClickListener(new OnClickListener() {
		    public void onClick(View v) {
		        if (((CheckBox) v).isChecked())
		        {
		            Toast.makeText(FriendTrackerController.this, "Enabled", Toast.LENGTH_SHORT).show();
		            try
		            {
		            	startupIntent = new Intent(FriendTrackerController.this,
		            		Class.forName("gatech.cs3251.project4.FriendTracker"));
		            }
		            catch(Exception e)
		            {
		            	Log.e("CHRIS", "Exception in controller enabling netcomm");
		            	Log.e("Exception:", e.getMessage());
		            }
		            Log.v("CHRIS", "Controller starting service");
		    		FriendTrackerController.this.startService(startupIntent);
		        }
		        else
		        {
		            Toast.makeText(FriendTrackerController.this, "Disabled", Toast.LENGTH_SHORT).show();
		            if(FriendTracker.isRunning == true)
		            {
		            	try
		            	{
		            		startupIntent = new Intent(FriendTrackerController.this,
		            				Class.forName("gatech.cs3251.project4.FriendTracker"));
		            	}
		            	catch(Exception e)
		            	{
		            		Log.e("CHRIS", "Exception in controller disabling netcomm");
		            		Log.e("Exception:", e.getMessage());
		            	}
		            	Log.v("CHRIS", "Controller stopping service");
		            	FriendTrackerController.this.stopService(startupIntent);
		            }
		        }
		    }
		});
	}
	
	public void launch(View v)
	{
    	try
    	{
    		startupIntent = new Intent(FriendTrackerController.this,
    				gatech.cs3251.project4.FriendViewer.class);
    	}
    	catch(Exception e)
    	{
    		Log.e("CHRIS", "Exception in starting Viewer");
    		Log.e("Exception:", e.getMessage());
    	}
    	Log.v("CHRIS", "Controller starting Viewer");
    	FriendTrackerController.this.startActivity(startupIntent);
	}
	
}
